package org.tekmob.remindpal.controller;

import java.util.Calendar;
import java.util.List;

import org.tekmob.remindpal.model.Reminder;
import org.tekmob.remindpal.model.ReminderDAO;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
/**
 * Temporary class
 * This activity will be called to
 * triggered all of alarm in the SQLite Database
 * 
 * @author Annisa
 *
 */
public class SettingAlarm extends Activity {
	Calendar cal;
	public ReminderDAO dao;
	List<Reminder> allReminder;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_alarm);
	
		//Initiate database
		dao = new ReminderDAO(this);
		//get all reminder in database
		List<Reminder> allReminder = dao.getReminder();
		cal = Calendar.getInstance();

		for (int i = 0; i < allReminder.size(); i++) {
			//Create an offset from the current time in which the alarm will go off.
			cal.set(Calendar.YEAR, allReminder.get(i).getYear());
			cal.set(Calendar.MONTH, allReminder.get(i).getMonth());
			cal.set(Calendar.DAY_OF_MONTH, allReminder.get(i).getDate());

			cal.set(Calendar.HOUR_OF_DAY, allReminder.get(i).getHour());
			cal.set(Calendar.MINUTE, allReminder.get(i).getMinute());

			//Create a new PendingIntent and add it to the AlarmManager
			Intent intent = new Intent(this, AlarmReceiverActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(),
					allReminder.get(i).getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
			AlarmManager am = 
					(AlarmManager) this.getSystemService(Activity.ALARM_SERVICE);
			am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
					pendingIntent);
			

		}
		
		/*cal.add(Calendar.SECOND, 5);
		//Create a new PendingIntent and add it to the AlarmManager
		Intent intent = new Intent(this, AlarmReceiverActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(),
				123, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager am = 
				(AlarmManager) this.getSystemService(Activity.ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
				pendingIntent);*/

	}
}
